package com.ys.controller.api;

import com.ys.vo.R;
import com.ys.config.MenuId;
import com.ys.entity.business.TpProductBasisComponent;
import com.ys.entity.business.TpShopAnalysisReportForms;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.business.TpProductBasicService;
import com.ys.service.business.YsbExportLogService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tpProductBasisChildNew")
public class ApiInterTpProductBasisChildNewController {
	public static Long MENU_ID = 28L;
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TpProductBasisComponent> commonApiService;
	@Autowired
	private TpProductBasicService tpProductBasicService;

	private static Long MENU_ID_BY_USER = 27L;

	@Autowired
	private YsbExportLogService ysbExportLogService;


	@RequiresPermissions("tpProductBasisComponent:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TpProductBasisComponent cond
			){
		cond.addField("A.num combination,A.BPrice ziprice,A.linkInMark biaoshi,A.remark remark")
		.addLeftJoin("TP_ProductBasis pb ON  pb.code = A.Pcode")
		.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = pb.VgsBm")
		.addField("THRO.CUser AS cUser");
		cond = commonApiService.dataAuth(MENU_ID, cond);
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 新增
	 */
	@PostMapping("/add")
    @ResponseBody
	public R add(HttpServletRequest request, @RequestParam Map<String, Object> params){
		return tpProductBasicService.add(params);
	}
	
	/**
	 * 导出
	 */
	@PostMapping("/export")
    @ResponseBody
	public com.ys.vo.R export(@ModelAttribute TpProductBasisComponent cond, String conditions) throws Exception{
		YsbExportLogService.ExportDto dto1 = ysbExportLogService.init(MENU_ID);
		try {
			cond.setPage(0).setSize(999999);

			List<Map<String, Object>> listData = cond.queryMap();



			List<YsCol> listField = new YsCol()
					.setMenuId(MENU_ID)
					.setFrontShow(YsColEnum.FrontShow.SHOW.value)
					.setOrderby("A.SortNo asc")
					.queryList();

			ExportExcelUtils<TpProductBasisComponent> util = new ExportExcelUtils<TpProductBasisComponent>(new TpProductBasisComponent());

			String toFilePath = util.exportExcel(listData, listField, "商品子件表");
			UploadService.UploadDto dto2 = commonApiService.uploadLocalToServer(toFilePath);
			ysbExportLogService.updateSuccess(dto1.getId(), dto2.getUrl());
			return com.ys.vo.R.ok("已经生成完毕");
		}catch (Exception e){
			ysbExportLogService.updateFail(dto1.getId());
			return com.ys.vo.R.error("生成失败，请联系客服");
		}
	}

	@RequestMapping("/searchStore")
	@ResponseBody
	public LayuiTableVoJA  searchStore(Model model, HttpServletRequest request, HttpServletResponse response,
									   @ModelAttribute TpProductBasisComponent cond){
		String mainTable = " SELECT B.Id, B.Code, B.Name,B.SubCodeMerge codeMerge,0 isParent FROM TP_ProductBasisComponent B UNION ALL  SELECT C.Id,C.Code, C.Name,C.CodeMerge codeMerge,1 isParent FROM TP_ProductActive C";

		cond.setMainTable("("+mainTable+")");
//		cond.clearFieldsSource();
		cond.setFields("A.Id,A.Code code, A.Name name,A.CodeMerge codeMerge,A.IsParent isParent");
		//排序
//		cond.setOrderby("A.InfoTime DESC, A.TotalOrder DESC");
		//权限
		cond = commonApiService.dataAuth(MENU_ID_BY_USER, cond);
		return commonApiService.searchLay(cond);
	}
}
